package com.ricky.handler.action;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.ricky.core.net.abs.Request;
import com.ricky.core.net.msg.Context;
import com.ricky.core.net.utils.Attributes;
import com.ricky.game.GameWorld;
import com.ricky.game.Hero;
import com.ricky.game.MsgComressionHelper;
import com.ricky.game.MsgHelper;
import com.ricky.game.Utils;
import com.ricky.game.scene.RoomController;
import com.ricky.game.scene.SceneBase;
import com.ricky.handler.IHandler;

public class Room implements IHandler {

	protected static Logger log = Logger.getLogger(Room.class);

	@SuppressWarnings("unchecked")
	@Override
	public void proccess(Request request, Map<String,Object> context) {

		@SuppressWarnings("rawtypes")
		Map msg =(Map) context;
		int type = (Integer) msg.get("t");
		Hero h = (Hero) request.getAttribute(Attributes.HERO);
		Map<String,Object> arg=null;
		switch (type) {
			case 0: // 创建房间
				String name=(String) msg.get("name");
				@SuppressWarnings("unchecked")
				Map<String,Object> roominfo=(Map<String, Object>) msg.get("info");
				createRoom(h,name,roominfo);
				break;
			case 1://加入房间
				int roomid=(Integer) msg.get("roomID");
				joinRoom(h,roomid);
				break;
			case 2://开始加载
				loadAll(h);
				break;
			case 3://有玩家加载完成
				enter(h);
				break;
			case 4://得到除自己外所有玩家所有信息
				getRoomHeros(h);
				break;
			case 5: //得到除自己外所有玩家指定信息
				String key =(String)msg.get("arg");
				getRoomHerosAtt(h,key);
				break;
			case 10://广播所有 scence
				arg=(Map<String, Object>) msg.get("arg");
				broadcast(h,null,arg);
				break;
			case 11://广播除了自己自己
				arg=(Map<String, Object>) msg.get("arg");
				broadcast(h,h,arg);
				break;
			case 12://广播waiting room
				arg=(Map<String, Object>) msg.get("arg");
				broadcastWaiting(h,null,arg);
				break;
			case 13://广播waiting room 除了自己
				arg=(Map<String, Object>) msg.get("arg");
				broadcastWaiting(h,h,arg);
				break;
				
			case 20://设置英雄信息
				arg=(Map<String, Object>) msg.get("arg");
				setHeroInfo(h,(Map<String,Object>) arg);
				break;
//			case 21://设置英雄位置信息
//				Object pos=msg.get("arg");
//				setHeroPos(h,(Map<String, Object>) pos);
//				break;
				
		}
	}
	
	/**
	 * 设置英雄信息
	 * @param h
	 * @param info
	 */
	private void setHeroInfo(Hero h,Map<String,Object> msg)
	{
		for(Map.Entry<String, Object>  entry: msg.entrySet())
		{
			h.setAttribute(entry.getKey(), entry.getValue());
			log.info("设置信息:"+entry.getKey());
		}
	}
	
//	/**
//	 * 设置英雄位置
//	 * @param h
//	 * @param info
//	 */
//	private void setHeroPos(Hero h,Map<String,Object> info)
//	{
//		double x=(Double) info.get("x");
//		double y=(Double) info.get("y");
//		double z=(Double) info.get("z");
//		double rotateY=(Double) info.get("rotateY");
//		h.getGraphic().setX(x);
//		h.getGraphic().setY(y);
//		h.getGraphic().setZ(z);
//		h.getGraphic().setRotateY(rotateY);
//		log.info("位置:"+info);
//
//	}
	
	/**
	 * 得到房间里面所有英雄的信息
	 * @param h
	 */
	private void getRoomHeros(Hero h)
	{
		Map<String,Object> data1=new HashMap<String,Object>();
		data1.put("cmd", "r.getHero");
		
		List<Object>  heroList=new ArrayList<Object> ();
		SceneBase rc=h.getCurrentScene();
		if(rc!=null)
		{
			Collection<Hero> heros=rc.getOnline().values();
			for(Hero h1:heros)
			{
				if(h1!=h)
				{
					Object info=MsgComressionHelper.heroDataMsg(h1);
					heroList.add(info);
				}
			}

			data1.put("arg", Utils.changeListToArray(heroList));
		}
		
		MsgHelper.sendMsg(h.getRequest(), data1);
	}
	
	/**
	 * 所有英雄的指定信息
	 * @param h
	 * @param key
	 */
	private void getRoomHerosAtt(Hero h,String key)
	{
		Map<String,Object> data1=new HashMap<String,Object>();
		data1.put("cmd", "r.updateHero");
		
		List<Object>  heroList=new ArrayList<Object> ();
		SceneBase rc=h.getCurrentScene();
		if(rc!=null)
		{
			Collection<Hero> heros=rc.getOnline().values();
			for(Hero h1:heros)
			{
				if(h1!=h)
				{
					Object info=MsgComressionHelper.heroAttMsg(h1,key);
					heroList.add(info);
				}
			}

			data1.put("arg", Utils.changeListToArray(heroList));
		}
		
		MsgHelper.sendMsg(h.getRequest(), data1);
	}
	
	private void createRoom(Hero h,String name,Map<String,Object> roominfo)
	{
		if(h.getCeatedRoom()!=null)
			GameWorld.getInstance().removeRoomFormWorld(h.getCeatedRoom());
		
		int id=RoomController.IDGenerate.incrementAndGet();
		RoomController rc=new RoomController();
		rc.init();
		rc.setId(id);
		rc.setName(name);
		rc.setSceneInfo(roominfo);
		rc.setCreator(h);
		h.setCeatedRoom(rc);
		GameWorld.getInstance().addRoomToWorld(rc);
		rc.joinWaitingRoom(h, 0, 0, 0);
		Map<String, Object> room=MsgComressionHelper.roomInfo(rc);
		room.put("cmd", "r.roomCreated");
		MsgHelper.sendMsg(h.getRequest(), room);
		Pub.getRoomList(h);
	}
	
	private void joinRoom(Hero h,int roomId)
	{
		RoomController rc=GameWorld.getInstance().getRoom(roomId);
		if(rc!=null) //
		{
			if (!rc.isLocked())
			{
				if(h.getWaiting()!=null)
					h.getWaiting().removeFromWaitingRoom(h);
				
				rc.joinWaitingRoom(h,0,0,0);
				Map<String, Object> room=MsgComressionHelper.roomInfo(rc);
				room.put("cmd", "r.waitingRoomJoined");
				MsgHelper.sendMsg(h.getRequest(), room);
			}else
			{
				Map<String, Object> room=new HashMap<String, Object>();
				room.put("cmd", "r.roomLocked");
				MsgHelper.sendMsg(h.getRequest(), room);
			}
		}else
		{
			Map<String, Object> room=new HashMap<String, Object>();
			room.put("cmd", "r.roomNotExist");
			MsgHelper.sendMsg(h.getRequest(), room);
		}
	}
	
	/**
	 * 加载所有
	 */
	private void loadAll(Hero h)
	{
		RoomController rc=h.getCeatedRoom();
		
		if(rc==h.getWaiting() && rc!=null)
		{
			rc.setLocked(true);
			Map<String, Object> room=new HashMap<String, Object>();
			room.put("cmd", "r.beginLoading");
			rc.broadcastWaitingRoom(null, room);
		}
	}
	
	/**
	 * 加入场景
	 * @param h
	 */
	private void enter(Hero h)
	{
		RoomController waiting=(RoomController) h.getWaiting();
		if(waiting!=null)
		{
			waiting.enter(h);
		}
	}
	
	private void broadcast(Hero h,Hero except,Map<String,Object> arg)
	{
		SceneBase room=h.getCurrentScene();
		if(room!=null)
			room.broadcast(except, arg);
	}
	
	private void broadcastWaiting(Hero h,Hero except,Map<String,Object> arg)
	{
		SceneBase room=h.getWaiting();
		if(room!=null)
			room.broadcast(except, arg);
	}

}
